package com.atguigui.leetcode1;

/**
 * 1041.困于环中的机器人
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/4/11 8:55
 */
public class P1041RobotBoundedInCircle {
    public static void main(String[] args) {
        Solution solution = new P1041RobotBoundedInCircle().new Solution();
        // TO TEST
    }

    class Solution {
        public boolean isRobotBounded(String instructions) {
            int[][] direc = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
            int direcIndex = 0;
            int x = 0, y = 0;
            int n = instructions.length();
            for (int idx = 0; idx < n; idx++) {
                char instruction = instructions.charAt(idx);
                if (instruction == 'G') {
                    x += direc[direcIndex][0];
                    y += direc[direcIndex][1];
                } else if (instruction == 'L') {
                    direcIndex += 3;
                    direcIndex %= 4;
                } else {
                    direcIndex++;
                    direcIndex %= 4;
                }
            }
            return direcIndex != 0 || (x == 0 && y == 0);
        }
    }
}
